<?php
namespace jy_weishop;
defined('BY_JYA') or exit('error');
/*
	获取某商户下单结算数据
*/
class get_jiesuan extends \plugin{
	/*
		获取数据
	*/
	public function get_merchs($return=array()){
		global $_RQ;
		$shopset = $this->plugin_setting('shopset');
		$carrier_list = pdo_getall('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
		$goodsid = intval($_RQ['goodsid']);
		//来自商户的订单
		$merchid = intval($_RQ['merchid']);
		if($_RQ['storeid']){
			$_SESSION['store_id']=intval($_RQ['storeid']);
		}
		$store_id=$_SESSION['store_id'];
		$sendtype=intval($_RQ['sendtype']);
		//判断是不是点餐
		$_SESSION['is_dish']=intval($_RQ['is_dish']);
		$merchs=array();
		if($goodsid){
			$_SESSION['goodsid']=$goodsid;
			//直接购买。
			$goods=$this->get_goods($goodsid,intval($_RQ['num']),intval($_RQ['optionsid']),intval($_RQ['roomid']),$shopset,$carrier_list);
			$post['total'] = intval($_RQ['num']);
			$post['invoice'] = $goods['invoice'];
			$order=array(
				'post'=>$post,
				'sendtype'=>$sendtype,
				'type'=>'one',
				'list'=>array($goods),
				'sendtypes'=>$goods['sendtypes']?:array(),
			);
			$merchs[]=$order;
		}else{
			//从购物车购买
			$sendtype=1;//类型只能是快递配送
			if($_RQ['isbuyto']){
				$cart_list=$_SESSION['cart_list'];
				$_SESSION['cart_list']='';
				if(!$cart_list){
					exi('您没有选中商品！','error');
				}
			}else{
				$cartparms=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'selected'=>1);
				$selectmerch=$this->session('selectmerch');
				if(!empty($selectmerch)){
					$merchids=array_keys($selectmerch);
					if($selectmerch['-1']){
						$merchids[]=0;
					}
					$cartparms['merchid']=$merchids;
				}
				if($merchid==-1){
					$cartparms['merchid']=0;
				}elseif($merchid>0){
					$cartparms['merchid']=$merchid;
				}
				$cart_list=pdo_getall('jy_weishop_member_cart',$cartparms,array('id','goodsid','total','optionid','merchid'));
				if(!$cart_list){
					exi('您的购物车没有选中商品！','error');
				}
			}
			$diyfields=array();
			$nowplugin=$_SESSION['current_plugin']?:'jy_weishop';
			$menus_index=$_SESSION['menus_index']?:$nowplugin.'/index/';
			foreach($cart_list as $k=>$cart){
				$goods=$this->get_goods($cart['goodsid'],intval($cart['total']),intval($cart['optionid']),intval($_RQ['roomid']),$shopset,$carrier_list);
				$cart=array_merge($goods,$cart);
				$post['invoice'] = $goods['invoice'];
				$merch='';
				if(empty($merchs[$cart['merchid']])){
					if($cart['merchid']){
						$merch=pdo_get('jy_merch_user',array('id'=>$cart['merchid']),array('id','merchname'));
					}
					$merchs[$cart['merchid']]=array(
							'post'=>$post,
							'total'=>0,
							'totalprice'=>0,
							'typename'=>$cart['merchid']?'商户':'平台',
							'merchid'=>$cart['merchid']?:-1,
							'merch'=>$merch?:array('merchname'=>'平台自营','id'=>0),
							'url'=>$cart['merchid']?'jy_merch/myshop//id='.$cart['merchid']:$menus_index,
							'sendtypes'=>$goods['sendtypes']?:array(),
					);
				}
				$cart['cartid']=$cart['id'];
				$cart['id']=$goods['id'];
				if($goods['sendtypes']){
					$merchs[$cart['merchid']]['sendtypes']=$goods['sendtypes'];
				}
				if(!$goods['type']){
					$merchs[$cart['merchid']]['issendtype']=1;
				}
				$merchs[$cart['merchid']]['list'][]=$cart;
			}
		}
		foreach($merchs as &$mv){
			$mv=$this->up_merchitem($mv);
		}
		return $merchs;
	}
	/*
		刷新商品
	*/
	public function get_goods($goodsid=array(),$num=1,$optionsid=0,$roomid=0,$shopset=array(),$carrier_list=array()){
			$goodsfile=array('id','fang_credit2','no_de_sales','isdiyprice','diyprice','invoice','type','buylevels','buygroups','title','merchid','status','pcate','total','subtitle','image','video','price','totalcnf','weight','active_plugin','active_id','storeids','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','isverify','diyfields','api_id','api_pluginname');
			$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid),$goodsfile);
			if(!$goods){
				exi('指定商品不存在！','error','goback');
			}
			$goods['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']),'weight');
			$goods['roomid']=$roomid;
			if($_SESSION['is_tomedia']){
				$goods['image']=$goods['image']?tomedia($goods['image']):'';
			}
			$goods['diyfields']=$this->model('goods')->get_diyformfields($goodsid);
			if($goods['diyfields']){
				$diyfields=$goods['diyfields'];
			}
			//配送设置
			$goodsdispatch=$this->model('goods')->get_dispatch($goodsid);
			$goods=array_merge($goods,$goodsdispatch);
			//如果来自其他商户分销（修改商户ID）
			$frommerchid=$this->session('frommerchid');
			$goods['merchid']=$frommerchid?$frommerchid:$goods['merchid'];
			$goods['goodsid']=$goods['id'];
			$goods['storeid']=$store_id;
			$goods['optionid']=$optionsid;
			$goods=$this->model('activity')->get_upgoods($goods);
			if($goods['active']['dian_mid']){
					$goods['merchid']=pdo_getcolumn('jy_merch_user',array('mid'=>$goods['active']['dian_mid'],'uniacid'=>$_SESSION['uniacid']),'id');
					$merchid=$goods['merchid'];
			}
			//重置下单数量
			$goods['goodstotal']=$goods['total'];
			$goods['total']=$num;
			if($optionsid){
				$return['post']['optionid'] = $optionsid;
				if(!$goods['options']){
						exi('指定规格的商品不存在！','error','goback');
				}
				$goods['price']=$goods['options']['price'];
				$specs = explode("_", $goods['options']['specs']);
				$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specsall){
						$optiontitle='';
						foreach($specsall as $k=>&$sp){
							if($k==0){
								$optiontitle .=$sp['title'];
							}else{
								$optiontitle .='，'.$sp['title'];
							}
						}
				}
				$goods['optiondata']= $optiontitle;
			}else{
				$goods['optionid']=0;
			}
			if($goods['active_plugin'] && !$optionsid){
					//商品在做活动，检查下库存是否不足
					$this->model('activity')->check_stock($goods['id'],$goods['active_plugin'],$goods['active_id'],$goods['total']);
			}
			if($goods['type']==1 || $goods['type']==2){
				//虚拟商品
				$goods['sendtype']=3;
			}elseif($goods['type']==3){
				//计时计次服务，发货类型必须是上门核销
				$goods['sendtype']=2;
			}elseif($goods['type']==0 || $goods['isverify']){
				//实体商品，查找是否有门店，如果有门店则表示支持自提，否则只能是快递配送
				if(!$carrier_list || (($goods['merchid']<=0 || !$goods['merchid']) && !$goods['isverify'] && !$shopset['is_sendtype2'])){
					$goods['sendtype']=1;
				}else{
					$goods['sendtypes']=array(
						'1'=>'快递配送',
						'2'=>'自提',
					);
				}
			}
			return $goods;
	}
	/*
		刷新费用
	*/
	public function up_merchitem($merchitem=array()){
		if($merchitem['merchid']>0){
			if(empty($merchitem['merch'])){
				$merchitem['merch']=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchitem['merchid']));
			}
			$merchitem['ismake']=$merchitem['merch']['ismake'];
			if($merchitem['merch']['sendtype']){
				$merchitem['sendtype']=$merchitem['merch']['sendtype'];
			}
		}
		//统计订单商品总数和总价
		$total=0;
		$total_price=0.00;
		$total_weight=0;
		$buyxiu_de_price=0;
		$fang_credit2=0;
		foreach($merchitem['list'] as &$ov){
			//返现
			if($ov['fang_credit2']){
				if(substr($ov['fang_credit2'],-1)=='%'){
					$ov['fang_credit2']=($ov['total']*$ov['price']*substr($ov['fang_credit2'],0,-1))/100;
				}else{
					$ov['fang_credit2']=$ov['total']*$ov['fang_credit2'];
				}
			}
			$total+=$ov['total'];
			$fang_credit2+=$ov['fang_credit2'];
			$total_price+=$ov['total']*$ov['price'];
			$total_weight+=$ov['weight']*$ov['total'];
			$buyxiu_de_price+=$ov['buyxiu_de_price'];
			if($ov['api_data']){
				if(!is_array($ov['api_data'])){
					$ov['api_data']=json_decode($ov['api_data'],true);
				}
				if($ov['api_data']['no_paytype']){
					$merchitem['post']['setno_paytype']=str_array($ov['api_data']['no_paytype']);
				}
			}
		}
		$merchitem['fang_credit2']=$fang_credit2;
		$merchitem['total']=$total;
		$merchitem['total_price']=$total_price;
		$merchitem['total_weight']=round(($total_weight/1000)*1000,2);
		$merchitem['buyxiu_de_price']=$buyxiu_de_price;
		//计算运费
		$item['address']=array();
		$merchitem['dispatchprice']=0;
		$merchitem['sendtype']=$merchitem['sendtype']?:1;
		//刷新运费
		if(!$merchitem['sendtype'] || $merchitem['sendtype']==1 || $merchitem['sendtypes']){
			$up_dispatch=$this->model('order/dispatch')->up_dispatch($merchitem,$item,$merchitem['addressid'],$_SESSION['store_id'],$merchitem['merch']);
			$merchitem=$up_dispatch['order']?:$merchitem;
			$item=$up_dispatch['item']?:$item;
		}
		$kuaipeisong=$this->plugin_setting('kuaipeisong');
		if($kuaipeisong['isnoopen']){
			$merchitem['sendtype']=$merchitem['sendtype']==1?2:$merchitem['sendtype'];
			unset($merchitem['sendtypes'][1]);
		}
		if($merchitem['is_peisong']){
			$merchitem['sendtypes']=array(
					'1'=>'同城配送',
					'2'=>'到店自取',
			);
		}
		if($_SESSION['is_dish']){
			$merchitem['sendtypes']=array(
				'1'=>'外卖配送',
				'2'=>'到店自取',
			);
		}
		//会员折扣价
		$merchitem['level_de_price']=$this->model('activity')->get_level_de_price($merchitem);
		//运势折扣价
		$merchitem['luck_de_price']=$this->model('activity')->get_luck_de_price($merchitem);
		//满减优惠价格：
		$sale_enough=$saleset['enough'];
		if($sale_enough){
			foreach($sale_enough as $k=>$ev){
				if($ev['man']>$merchitem['total_price']){
					unset($sale_enough[$k]);
				}
			}
			paixu($sale_enough,'man','desc');
		}
		$merchitem['sale_enough']=$sale_enough;
		$merchitem['enough_de_price']=$sale_enough[0]['jian']?$sale_enough[0]['jian']:0.00;
		//送积分
		$merchitem['credit']=$this->model('activity')->get_buy_credit_order($merchitem);
		//积分抵扣
		$deduct_res=$this->model('activity')->get_deduct_credit_order($merchitem,$saleset,$member);
		$merchitem['deduct_credit']=$deduct_res['deduct_credit'];
		$merchitem['delcredit1']=$deduct_res['delcredit1'];
		//优惠券优化价格：
		$merchitem['coupon_de_price']=0.00;
		if(!$merchitem['no_coupon_discount']){
			//整理优惠券数据，先获取用户所有待使用优惠券，再从中筛选出未过期的优惠券且本次下单可用的优惠券！
			$newcoupons=$this->model('coupon')->getmycoupon_ing(1,$merchitem['total_price'],$merchitem['level_de_price'],$merchitem['list'],$merchitem['enough_de_price'],1);
			$merchitem['coupon_de_price']=$newcoupons[0]['de_price'];
			$merchitem['couponid']=$newcoupons[0]['id'];
			$merchitem['couponcount']=count($newcoupons);
		}
		$merchitem['coupons']=$newcoupons?$newcoupons:array();
		//提交数据
		$merchitem['post']['merchid'] = $merchitem['merchid'];
		$merchitem['post']['couponid'] = $newcoupons[0]['id'];
		$merchitem['post']['make_type'] = 0;
		$merchitem['post']['dayid'] = 0;
		$merchitem['post']['title5'] = $newcoupons[0]['coupon']['title5'];
		$merchitem['post']['deduct'] = $newcoupons[0]['coupon']['deduct'];
		$merchitem['post']['discount'] = $newcoupons[0]['coupon']['discount'];
		$merchitem['post']['backtype'] = $newcoupons[0]['coupon']['backtype'];
		$merchitem['post']['addressid'] = $item['addressid'];
		$merchitem['post']['sendtype'] = $merchitem['sendtype']?:1;
		$merchitem['post']['sendtime'] = strtotime("+2 day");
		$merchitem['post']['deliverytime'] = date("m月d日",strtotime("+2 day"));
		$merchitem['post']['diyfields'] = $diyfields;
		$merchitem['post']['dispatchprice']=$merchitem['dispatchprice'];
		$merchitem['post']['realname']=$merchitem['post']['address']['name'];
		$merchitem['post']['mobile']=$merchitem['post']['address']['mobile'];
		//配送
		$merchitem['deliverytime'] = $merchitem['post']['deliverytime'];
		//打包费
		$merchitem['out_upfee']=$merchitem['merch']['out_upfee']>0?$merchitem['merch']['out_upfee']:0;
		//整理最终价(暂不扣除优惠券)post是最终价，order是未减优惠券的价
		$merchitem['youprice'] = $merchitem['level_de_price'] + $merchitem['luck_de_price']+$merchitem['buyxiu_de_price'] + $merchitem['enough_de_price'] + $merchitem['deduct_credit'];
		$merchitem['endprice']=round($merchitem['total_price'] + $merchitem['dispatchprice'] + $merchitem['out_upfee'] - $merchitem['youprice'],2);
		$merchitem['no_dispatch_price']=round($merchitem['total_price'] + $merchitem['out_upfee'] - $merchitem['youprice'],2);
		$merchitem['endprice']=$merchitem['endprice']>=0?$merchitem['endprice']:0;
		$merchitem['post']['youprice']=intval(($merchitem['youprice']+$merchitem['coupon_de_price'])*100)/100;
		$merchitem['post']['endprice']=$merchitem['endprice']-$merchitem['coupon_de_price'];
		$merchitem['post']['endprice']=$merchitem['post']['endprice']>=0?$merchitem['post']['endprice']:0;
		$merchitem['post']['no_dispatch_price']=$merchitem['no_dispatch_price']- $merchitem['coupon_de_price'];
		$merchitem['no_dispatch_price2']=$merchitem['post']['no_dispatch_price'];
		$merchitem['post']['no_dispatch_price']=$merchitem['post']['no_dispatch_price']>=0?$merchitem['post']['no_dispatch_price']:0;
		$merchitem['no_dicard_endprice']=$merchitem['post']['endprice'];
		$merchitem['no_dicard_dispatch_endprice']=$merchitem['post']['no_dispatch_price'];
		//购物卡抵扣
		$last_cardfee=$this->model('goodscard')->getorderfee($merchitem['total_price'],$merchitem['level_de_price'],$merchitem['list'],$merchitem['enough_de_price']);
		if($last_cardfee>0){
			$merchitem['last_cardfee']=$last_cardfee;
			if($merchitem['last_cardfee']<=$return['post']['endprice']){
				$merchitem['di_cardfee']=$merchitem['last_cardfee'];
			}else{
				$merchitem['di_cardfee']=$merchitem['post']['endprice'];
			}
			$merchitem['post']['endprice'] -=$merchitem['di_cardfee'];
			if($merchitem['last_cardfee']<=$merchitem['post']['no_dispatch_price']){
				$merchitem['no_dispatch_cardfee']=$merchitem['last_cardfee'];
			}else{
				$merchitem['no_dispatch_cardfee']=$merchitem['post']['endprice'];
			}
			$merchitem['post']['no_dispatch_price'] -=$merchitem['no_dispatch_cardfee'];
		}
		/*
			赠品
			先根据订单金额找赠品，再根据商品找赠品
		*/
		$goods_gifts=$this->pdo_getall('goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>0));
		//$current_gift=array();
		if($goods_gifts){
			foreach($goods_gifts as $gift){
				$gift['onlinetime']=json_decode($gift['onlinetime'],true);
				if($gift['onlinetime']['tstart']<time() && $gift['onlinetime']['tend']>time() && $gift['orderprice'] && $gift['orderprice']<$merchitem['no_dispatch_price']){
					if(!$current_gift || $current_gift['orderprice']<$gift['orderprice']){
						$current_gift=$gift;
					}
				}
			}
		}
		if(!$current_gift){
			$goods_gifts=$this->pdo_getall('goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>1));
		}
		if($goods_gifts){
			foreach($goods_gifts as $gift){
				$gift['onlinetime']=json_decode($gift['onlinetime'],true);
				$gift['goodsid']=json_decode($gift['goodsid'],true);
				if($gift['onlinetime']['tstart']<time() && $gift['onlinetime']['tend']>time() && $gift['goodsid']){
					foreach($order['list'] as $goods){
						foreach($gift['goodsid'] as $gv){
							if($goods['goodsid']==$gv['id']){
								$current_gift=$gift;
								break;
							}
						}
					}
				}
				if($current_gift){
					break;
				}
			}
		}
		if($current_gift){
			$merchitem['gift_id']=$current_gift['id'];
			$merchitem['gifts']=json_decode($current_gift['gifts'],true);
			foreach($merchitem['gifts'] as &$gv){
				$gv['price']=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['id'],'uniacid'=>$_SESSION['uniacid']),'price');
				if($_SESSION['is_tomedia']){
					$gv['image']=$gv['image']?tomedia($gv['image']):'';
				}
			}
		}
		return $merchitem;
	}
	/*
		提交数据
	*/
	public function postorder($order=array(),$post=array()){
		//公共参数
		if($post['out_time']){
			//配送时间
			$days = array(
					0=>date("Y-m-d"),
					1=>date("Y-m-d",strtotime("+1 day")),
					2=>date("Y-m-d",strtotime("+2 day")), 
					3=>date("Y-m-d",strtotime("+3 day")), 
					4=>date("Y-m-d",strtotime("+4 day")),
					5=>date("Y-m-d",strtotime("+5 day")),
					6=>date("Y-m-d",strtotime("+6 day")),
			);
			$dayid=intval($post['dayid']);
			$post['out_time']=$days[$dayid].' '.$post['out_time'];
		}
		$order['merchid']=$order['merchid']>0?$order['merchid']:0;
		$post['merchid']=$post['merchid']>0?$post['merchid']:0;
		$new_order=array(
			'mid'=>$_SESSION['uid'],
			'goodsprice'=>$order['total_price'],
			'remark'=>trim($order['post']['remark']),
			'goods'=>$order['list'],
			'merchid'=>$order['merchid'],
			'sendtype'=>intval($post['sendtype'])?:1,
			'diyfields'=>ijson_encode($post['diyfields']),
			'out_time'=>trim($post['out_time']),
			'make_type'=>intval($post['make_type']),
			'is_peisong'=>$order['is_peisong'],
			'out_upfee'=>$order['out_upfee']?:0,
			'mobile'=>$post['sendtype']==1?$post['address']['mobile']:$post['mobile'],
			'realname'=>$post['sendtype']==1?$post['address']['name']:$post['realname'],
			'address'=>$post['address'],
		);
		$new_order['invoice']=array(
			'type'=>$post['invoicetype'],
			'goodstype'=>$post['invoicegoodstype'],
			'name'=>$post['invoicename'],
			'code'=>$post['invoicecode'],
			'email'=>$post['email'],
		);
		//会员折扣
		if(!$order['no_member_discount'] && $order['level_de_price']){
			$new_order['level_de_price']=$order['level_de_price'];
		}
		//运势折扣
		if($order['luck_de_price']){
			$new_order['luck_de_price']=$order['luck_de_price'];
		}
		//买家秀
		if($order['buyxiu_de_price']){
			$new_order['buyxiu_de_price']=$order['buyxiu_de_price'];
		}
		//满减
		$new_order['enough_de_price']=$order['enough_de_price'];
		//抵扣
		$new_order['deduct_credit']=$order['deduct_credit']?:0;
		$new_order['delcredit1']=$order['delcredit1']?:0;
		//优惠券
		$new_order['price']=$new_order['goodsprice']+$new_order['out_upfee']-$new_order['enough_de_price']-$new_order['level_de_price']-$new_order['luck_de_price']-$new_order['buyxiu_de_price']-$new_order['deduct_credit'];
		if(!$order['no_coupon_discount']){
			$couponid=intval($order['post']['couponid']);
			//获取优惠信息
			$getcouponfee=$this->model('coupon')->getorderfee($couponid,$new_order['price'],$new_order['goodsprice']);
			if($getcouponfee){
				$new_order=array_merge($new_order,$getcouponfee);
			}
		}
		//自动分配门店		
		$storeset=$this->plugin_setting('storeset');
		if($storeset['open_storeid'] && !$post['merchid'] && !$post['store_no']){
			$store_notitle=$storeset['store_notitle']?:'门店编号';
			exi('请输入'.$store_notitle.'！','error');
		}
		if($post['store_no'] && $post['store_no']!=888888 && $post['store_no']!=$storeset['store_no']){
			$post['storeid']=$this->model('store')->getno_storeid($post['store_no']);
		}
		if($storeset['open_storeid'] && ($post['store_no']==888888 || $post['store_no']==$storeset['store_no'])){
			$post['storeid']=0;
		}
		$new_order['storeid']=intval($post['storeid']);
		if($new_order['sendtype']==1){
			//快递配送
			$new_order['addressid']=intval($post['addressid']);
			if(!$new_order['addressid'] && $_SESSION['uid'] && !$new_order['address']['address']){
				exi('请先选择收货地址！','error');
			}
			if(!$_SESSION['uid'] && !$new_order['address']['address']){
				exi('请先完善收货地址！','error');
			}
			$new_order['address']=pdo_get('core_address',array('id'=>$new_order['addressid']));
			$new_order['mobile']=$new_order['mobile']?:$new_order['address']['mobile'];
			$pcd=$new_order['address']['pcd'];
			if($pcd){
				$pcd=json_decode($pcd,true);
				$new_order['dispatchprice']=$order['post']['dispatchprice'];
				$new_order['price']+=$order['post']['dispatchprice'];
			}		
			if($post['juli']){
				$merchid=pdo_getcolumn('jy_weishop_goods',array('id'=>$_SESSION['goodsid']),'merchid');
				$l=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('lat','lng'));
				if($l['lng'] && $l['lat']){
					$d=cfc('tools')->getdistance($post['lat_lng']['lng'],$post['lat_lng']['lat'],$l['lng'],$l['lat']);
					if($d/1000>$post['juli']){
						exi('超出配送距离,无法购买','error','/goods.detail//id='.$_SESSION['goodsid']);
					}
				}
			}		
		}elseif($new_order['sendtype']==2){
			//门店自提
			if(!$new_order['storeid'] && !$order['merchid']){
				exi('请先选择门店！','error');
			}
			$shopset = $this->plugin_setting('shopset');
			if((!$new_order['realname'] || !$new_order['mobile']) && $shopset['is_noordermobile']){
				exi('请输入联系人和电话！','error');
			}
		}elseif($new_order['sendtype']==3){
			//虚拟发货
		}
		//购物卡抵扣
		if(!$post['no_di_cardfee'] && $order['di_cardfee']>0){
			$last_cardfee=$this->model('goodscard')->getorderfee($order['total_price'],$order['level_de_price'],$order['list'],$order['enough_de_price']);
			if($last_cardfee>0){
				if($last_cardfee<=$new_order['price']){
					$new_order['di_cardfee']=$last_cardfee;
				}else{
					$new_order['di_cardfee']=$new_order['price'];
				}
				$new_order['price'] -=$new_order['di_cardfee'];
			}
		}
		$new_order['price']=$new_order['price']>0?$new_order['price']:0;
		if($new_order['price']<0){
			exi('订单金额不能小于零！','error');
		}
		if($order['gift_id']){
			$new_order['gift_id']=$order['gift_id'];
		}
		if($_SESSION['is_dish']){
			$new_order['isdish']=1;
		}
		//如果需要制作，配送状态为未制作！
		if($order['ismake']==1){
			$new_order['send_status']=11;
		}
		$order_id=$this->model('order')->create($new_order);
		$params=array(
			'id'=>$order_id,
			'title'=>'购买商品',
			'fee'=>$new_order['price'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_weishop/order.create/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>$post['success_url']?:'jy_weishop/order.list/',
			'error_url'=>$post['error_url']?:'jy_weishop/order.list/',
		);
		//虚拟设置
		$plugin_xuni=$this->model('activity')->plugin_xuni($new_order);
		if(count($plugin_xuni)){
			$params['plugin_xuni']=$plugin_xuni;
		}
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$order_id));
		$res=array(
			'pay_id'=>$payid,
			'oid'=>$order_id,
			'plugin_xuni'=>$plugin_xuni,
			'fee'=>$new_order['price'],
		);
		return $res;
	}
}